/*
 * This file is part of andExplorer, android.ovhoo.com
 * Copyright (C) 2007 Mohamed ZALIM <mzalim@ovhoo.com>
 *
 * andExplorer is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * andExplorer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/gpl.html>.
 */
package com.ovhoo.android.fiveexplore;



import com.ovhoo.android.fiveexplore.R;

import com.ovhoo.android.file.CurentDirManager;
import com.ovhoo.android.file.FileInterface;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;

import com.ovhoo.android.file.MimeTypes;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * This class gives the list of the files in a directoy as icons/elements
 * @author mzalim
 *
 */
public class ContentAdapter extends BaseAdapter{

	private Activity context = null;
	private CurentDirManager fileManager; //Managing files
	private AlertDialog alertDialog;
	private TextView curentDirPath;
	private MimeTypes mimeTypes;
	
	/**
	 * Default constructor
	 * @param context
	 */
	public ContentAdapter(Activity context, String root, TextView curentDirPath){
		this.context = context;
		this.curentDirPath = curentDirPath;
		this.mimeTypes = MimeTypes.getInstance(context);
		
		this.fileManager = new CurentDirManager(this, root);
		
		this.curentDirPath.setText(this.fileManager.pwd());
		this.buildAlertDialog();
		
	}
		
	Handler handler=new Handler() {
		@Override
		public void handleMessage(Message msg) {
			
			if (msg != null){
				
				this.mkdir((String) msg.obj);
			}
			
		}
		
		protected void mkdir(String basename){
			if (!fileManager.mkdir(basename)){
				showAlertMessage(context.getResources().getString(R.string.OPEN_FILE_CANNOT_MKDIR));
			}
			
		}
	};
	
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return this.fileManager.getFilesList().size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View curentView, ViewGroup parent) {
		LayoutInflater inflater=context.getLayoutInflater();
		View _elementView = inflater.inflate(R.layout.icon, null);
		
		//Get the file
		FileInterface _file = this.fileManager.getFilesList().get(position);
		
		//Create the icon
		TextView _text = (TextView) _elementView.findViewById(R.id.ICON_FILE_TEXT);
		_text.setText(_file.getName());
		
		ImageView _image = (ImageView) _elementView.findViewById(R.id.ICON_FILE_ICON);
		_image.setImageResource(this.mimeTypes.getFileIcon(_file));
							
		return(_elementView);
	}
	
	
	/**
	 * Calledddd in order to update the view
	 */
	public void notifyDataSetChanged(){	
		super.notifyDataSetChanged();
		
		if (this.fileManager != null ){
			this.curentDirPath.setText(this.fileManager.pwd());
		}
		
	}
	
	
	public void createDirectory(){
		//Create Directory
		NewDirDialog _dialog = new NewDirDialog(this.context, this.handler.obtainMessage());
		_dialog.show();
	}

	/**
	 * Methode called when a view is clicked by the user
	 * @param view
	 * @param position
	 * @param id
	 */
	public void onItemClicked(View view, int position, long id) {
		FileInterface _file = this.fileManager.getFilesList().get(position);
		
		if (_file.isDirectory()){
			this.fileManager.cd(_file.getPath());
		}
		else{
			String _mimeType = mimeTypes.getMimeType(_file);
			if (_mimeType == null){
				OpenFileDialog _openFileDialog = new OpenFileDialog(this.context,this.fileManager, _file);
				_openFileDialog.show();
			}
			else if (_mimeType.startsWith("audio")){
				AudioPlayerDialog _audioPlayerDialog = new AudioPlayerDialog(this.context,this.fileManager, _file);
				_audioPlayerDialog.show();
			}
			else if (_mimeType.startsWith("image")){
				ImageViewerDialog _imageViewerDialog = new ImageViewerDialog(this.context,this.fileManager, _file);
				_imageViewerDialog.show();
			}
			else{
				OpenFileDialog _openFileDialog = new OpenFileDialog(this.context,this.fileManager, _file);
				_openFileDialog.show();
			}
			
		}
	}
	
	/**
	 * Methode called to gu up
	 */
	public void go_up() {
		this.fileManager.go_up();
	}
	
	protected void buildAlertDialog(){
		//Instanciation of the alert dialog
		this.alertDialog = new AlertDialog.Builder(this.context).create();
		//_alertDialog.setTitle(this.getTitle());
		this.alertDialog.setButton(this.context.getResources().getString(R.string.COMMON_STRINGS_MSG_OK), new DialogInterface.OnClickListener() {
		      public void onClick(DialogInterface dialog, int which) {
		        return;
		      } }); 
	}
	
	/**
	 * Display an alert message
	 * @param msg the alert message to be displayed
	 */
	protected void showAlertMessage(String msg){
		
		this.alertDialog.setMessage(msg);
		this.alertDialog.show();
	}
	
	
}
